import type { VbenFormProps, VbenFormSchema } from '#/adapter/form';
import { z } from '#/adapter/form';
import { validationRules } from '#/utils/validationRules';

// 编辑弹窗表单配置
export const passwordFormSchema: VbenFormSchema[] = [
  {
    component: 'InputPassword',
    componentProps: {
      placeholder: '请输入旧密码',
      maxLength: 18,
      allowClear: true,
    },
    fieldName: 'oldPassword',
    label: '旧密码',
    rules: z
      .string()
      .min(1, { message: validationRules.password.tips })
      .regex(validationRules.password.reg, validationRules.password.tips),
  },
  {
    component: 'InputPassword',
    componentProps: {
      placeholder: '请输入新密码',
      maxLength: 18,
      allowClear: true,
    },
    fieldName: 'password',
    label: '新密码',
    dependencies: {
      triggerFields: ['password'],
      rules: (values) => {
        return z
          .string()
          .regex(validationRules.password.reg, validationRules.password.tips)
          .refine(
            (password) => {
              return password !== values.oldPassword;
            },
            {
              message: '新密码不能与旧密码一致',
            },
          );
      },
    },
    rules: z
      .string()
      .min(1, { message: validationRules.password.tips })
      .regex(validationRules.password.reg, validationRules.password.tips),
  },
  {
    component: 'InputPassword',
    componentProps: {
      placeholder: '请再次输入新密码',
      maxLength: 18,
      allowClear: true,
    },
    fieldName: 'newPassword',
    label: '确认密码',
    dependencies: {
      triggerFields: ['newPassword'],
      rules: (values) => {
        return z
          .string()
          .regex(validationRules.password.reg, validationRules.password.tips)
          .refine(
            (newPassword) => {
              return newPassword === values.password;
            },
            {
              message: '确认密码必须与新密码一致',
            },
          );
      },
    },
    rules: z
      .string()
      .min(1, { message: validationRules.password.tips })
      .regex(validationRules.password.reg, validationRules.password.tips),
  },
];
